package com.hzbank.aaronkotlin

import android.graphics.Bitmap
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnLongClickListener
import android.widget.SeekBar
import android.widget.Toast
import com.aaron.smartktv.core.tool.XGravity
import com.aaron.smartktv.core.tool.XPenType
import com.aaron.smartktv.helper.saveXArtBoardToLocal
import com.hzbank.aaronkotlin.databinding.ActivityArtBoardBinding
import kotlin.random.Random


class ArtBoardActivity: BaseActivity<ActivityArtBoardBinding>() {

    private var inputTextDialog: InputTextDialog? = null

    override fun getBinding(): ActivityArtBoardBinding = ActivityArtBoardBinding.inflate(
        LayoutInflater.from(this))

    override fun parseView() {

        val bg: Bitmap = Bitmap.createBitmap(getScreenSize().x, getScreenSize().y, Bitmap.Config.ARGB_8888)

        bg.eraseColor(Color.parseColor("#ffffff"))

        xBing?.xartboard?.loadBitmap(bg, gravity = XGravity.GravityStart)

        xBing?.pen?.setOnClickListener {

            xBing?.xartboard?.setPenType(XPenType.LINE)
            if (xBing?.flControl?.visibility == View.VISIBLE) {
                xBing?.seekbarEraser?.progress = xBing?.xartboard?.getPenWidth()!!.toInt()
            }

        }

        xBing?.eraser?.setOnClickListener {

            if (xBing?.flControl?.visibility == View.VISIBLE) {
                xBing?.seekbarEraser?.progress = xBing?.xartboard?.getPenEraserWidth()!!.toInt()
            }
            xBing?.xartboard?.setPenType(XPenType.ERASER)

        }

        xBing?.color?.setOnClickListener {
            val nextInt =
                Color.argb(255, Random.nextInt(255), Random.nextInt(255), Random.nextInt(255))
            xBing?.xartboard?.setPenColor(nextInt)
            xBing?.color?.setTextColor(nextInt)
        }

        xBing?.retract?.setOnClickListener {

            xBing?.xartboard?.retract()
        }

        xBing?.retrieve?.setOnClickListener {

            xBing?.xartboard?.retrieve()
        }

        xBing?.reset?.setOnClickListener {
            xBing?.xartboard?.reset()
        }

        xBing?.close?.setOnClickListener {

            xBing?.flControl?.visibility = View.GONE
        }

        xBing?.markLabel?.setOnClickListener {

            inputTextDialog = InputTextDialog(this@ArtBoardActivity, 200, 300)
            inputTextDialog?.setInputCallback(object : InputTextDialog.Companion.InputCallback{
                override fun markLabel(label: String) {

                    ("标注内容1="+label).showLog()
                    //绘制
                    xBing?.xartboard?.setMarkLabel(label)

                }
            })
            //标注
            inputTextDialog?.showLoding()

        }

        xBing?.save?.setOnClickListener {

            saveXArtBoardToLocal( xBing?.xartboard?.getBitmap()!!, "${filesDir}/${System.currentTimeMillis()}.png") { bool, _ ->
                if (bool) {
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show()
                }
            }
        }

        xBing?.rect?.setOnClickListener {
            xBing?.xartboard?.setPenType(XPenType.RECT)
        }

        xBing?.square?.setOnClickListener {
            xBing?.xartboard?.setPenType(XPenType.SQUARE)
        }
        xBing?.oval?.setOnClickListener {
            xBing?.xartboard?.setPenType(XPenType.OVAL)
        }
        xBing?.circle?.setOnClickListener {
            xBing?.xartboard?.setPenType(XPenType.CIRCLE)
        }

        xBing?.pen?.setOnLongClickListener(object : OnLongClickListener{
            override fun onLongClick(v: View?): Boolean {

                xBing?.flControl?.visibility = View.VISIBLE
                xBing?.seekbarEraser?.max = 50
                xBing?.seekbarEraser?.progress = xBing?.xartboard?.getPenWidth()!!.toInt()
                xBing?.seekbarEraser?.setOnSeekBarChangeListener(object :
                    SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(
                        seekBar: SeekBar?,
                        progress: Int,
                        fromUser: Boolean
                    ) {

                    }

                    override fun onStartTrackingTouch(seekBar: SeekBar?) {

                    }

                    override fun onStopTrackingTouch(seekBar: SeekBar) {
                        xBing?.xartboard?.setPenWidth(seekBar.progress.toFloat())
                    }

                })

                return true
            }

        })

        xBing?.eraser?.setOnLongClickListener(object : OnLongClickListener{
            override fun onLongClick(v: View?): Boolean {

                xBing?.flControl?.visibility = View.VISIBLE
                xBing?.seekbarEraser?.max = 50
                xBing?.seekbarEraser?.progress = xBing?.xartboard?.getPenEraserWidth()!!.toInt()
                xBing?.seekbarEraser?.setOnSeekBarChangeListener(object :
                    SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(
                        seekBar: SeekBar?,
                        progress: Int,
                        fromUser: Boolean
                    ) {

                    }

                    override fun onStartTrackingTouch(seekBar: SeekBar?) {

                    }

                    override fun onStopTrackingTouch(seekBar: SeekBar) {
                        xBing?.xartboard?.setPenEraserWidth(seekBar.progress.toFloat())
                    }

                })

                return true
            }

        })

    }

    override fun onDestroy() {
        inputTextDialog?.hiddenLoading()
        super.onDestroy()
    }
}